package com.meng.ioc_demo;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;

import com.meng.baselibrary.adapter.BaseRecycleViewAdapter;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2017/5/4.
 */

public class BaseRecyclerViewAdapterTestAty extends AppCompatActivity implements BaseRecycleViewAdapter.OnItemClickListener {
    private RecyclerView recyclerView;
    private MyAdapter mAdapter;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_baserecycleviewadapter_test);
        recyclerView = (RecyclerView) findViewById(R.id.recyclerView);
        LinearLayoutManager layoutManager = new LinearLayoutManager(this);
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        recyclerView.setLayoutManager(layoutManager);

        mAdapter = new MyAdapter(this);
        mAdapter.setOnItemClickListener(this);
        mAdapter.addHeaderView(LayoutInflater.from(this).inflate(R.layout.v_header1,null));
        mAdapter.addHeaderView(LayoutInflater.from(this).inflate(R.layout.v_header2,null));
        mAdapter.addFooterView(LayoutInflater.from(this).inflate(R.layout.v_footer,null));
        recyclerView.setAdapter(mAdapter);
        mAdapter.setDatas(getDatas());
    }
    private List<Data> getDatas() {
        List<Data> datas = new ArrayList<>();
        for(int i = 1;i<= 50;i++){
            datas.add(new Data("数据"+i));
        }
        return datas;
    }

    @Override
    public void onItemClick(int recycleViewPosition, int itemPosition, View view, Object data) {
        Toast.makeText(this, mAdapter.getItem(itemPosition).title, Toast.LENGTH_SHORT).show();
    }

    class Data{
        String title;

        public Data(String title) {
            this.title = title;
        }
    }
    class MyAdapter extends BaseRecycleViewAdapter<Data>{
        public MyAdapter(Context context) {
            super(context);
        }
        private int itemLayoutId = R.layout.item_text;
        @Override
        protected int getItemViewLayout(int position) {
            return itemLayoutId;
        }
        @Override
        protected RecyclerView.ViewHolder onCreateItemViewHolder(ViewGroup parent, int viewType) {
            View view = getInflater().inflate(viewType,null);
            return new ItemViewHolder(view);
        }
        @Override
        protected void onBindItemViewHolder(RecyclerView.ViewHolder holder, int position) {
            ItemViewHolder viewHolder = (ItemViewHolder) holder;
            viewHolder.textView.setText(getItem(position).title);
        }
        class ItemViewHolder extends BaseRecycleViewAdapter.BaseViewHolder{
            TextView textView;
            public ItemViewHolder(View itemView) {
                super(itemView);
                textView = (TextView) itemView.findViewById(R.id.textView);
            }
        }
    }
}
